package com.czy.blog.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czy.blog.entity.Function;
import com.czy.blog.service.FunctionService;
import com.czy.blog.service.MenuService;
import com.czy.blog.utils.JsonMapper;
import com.czy.blog.vo.FunctionVo;
import com.czy.blog.vo.PageVo;

@Controller
@RequestMapping("/func")
public class FunctionController {
	@Autowired
	private MenuService menuService;
	@Autowired
	private FunctionService funcService;

	@RequestMapping("/getPage")
	@ResponseBody
	public String getPage(Model model, String id,PageVo pageVo) {
		List<Function> funcList = funcService.findByMenuId(id,pageVo);
		List<FunctionVo> vos = funcs2Vos(funcList);
		return new JsonMapper().toJson(vos);
	}

	/**
	 * List<Function>  -->   List<FunctionVo>
	 * @param funcList
	 */
	private List<FunctionVo> funcs2Vos(List<Function> funcList)
	{
		List<FunctionVo> vos = new ArrayList<FunctionVo>();
		for(Function func : funcList){
			FunctionVo vo = new FunctionVo();
			vo.setCz(func.getId());
			BeanUtils.copyProperties(func, vo);
			vos.add(vo);
		}
		return vos;
	}
	
	/**批量删除权限*/
	@RequestMapping("/batchDelete")
	@ResponseBody
	public String batchDelete(Model model, String[] ids) {
		funcService.delete(Arrays.asList(ids));
		return "success";
	}

	/** 添加权限页面*/
	@RequestMapping("/add/{menuId}") 
	public String add(Model model, @PathVariable String menuId) {
		model.addAttribute("func", new Function());
		model.addAttribute("action", "save");
		return "/func/func_edit";
	}
	
	/** 保存权限*/
	@RequestMapping("/save") 
	@ResponseBody
	public String save(Model model, Function func,String menuId) {
		funcService.save(func,menuId);
		return "success";
	}
	
	/** 编辑权限页面*/
	@RequestMapping("/edit/{funcId}") 
	public String edit(Model model, @PathVariable String funcId) {
		model.addAttribute("func", funcService.findById(funcId));
		model.addAttribute("action", "update");
		return "/func/func_edit";
	}
	
	/** 更新权限*/
	@RequestMapping("/update")
	@ResponseBody
	public String update(Model model, @ModelAttribute("updateObj") Function func) {
		funcService.update(func);
		return "success";
	}

	/** 进入权限管理主页面*/
	@RequestMapping("/main")
	public String main() {
		return "/func/func_main";
	}

	/** 加载菜单树*/
	@RequestMapping("/tree")
	@ResponseBody
	public String menuTree() {
		return menuService.generalJsonById2();
	}
	
	/**
	 * 所有RequestMapping方法调用前的Model准备方法, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 表单请求参数bind=true才执行，仅对update()有效（提交请求中不包含password, registerDate, salt, roles属性值，须预绑定到model）
	 */
	@ModelAttribute
	public void prepare(@RequestParam(defaultValue = "false") Boolean bindable, String id, Model model) {
		if (bindable) {
			model.addAttribute("updateObj", funcService.findById(id));
		}
	}
}
